﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;
using DotNetWikiBot;
using System.Threading;

namespace AlexTranslator
{
    public partial class MatchRedLink : WikiWinForm
    {
        private static object syncObj = new object();

        string articleTitle= "";
        PageList foundList = null;
        ManualResetEvent eventHandle = new ManualResetEvent(false);

        public MatchRedLink(string link, PageList found)
        {
            this.articleTitle = link;
            foundList = found;
            InitializeComponent();
            ThreadPool.QueueUserWorkItem(new WaitCallback(FindMatches));
            
        }
        public MatchRedLink()
        {
            InitializeComponent();
        }

        private void MatchRedLink_Load(object sender, EventArgs e)
        {
            
            ThreadPool.QueueUserWorkItem(new WaitCallback(BindList));
        }

        private void FindMatches(object o)
        {
            Page p = null;
            lock (syncObj)
            {
                p = new Page(UkSite, articleTitle);
                p.Load();
            }

            if (string.IsNullOrEmpty(p.text))
            {
                //foundList= FillFoundList();
                eventHandle.Set();
            }
            
        }

        private void BindList(object o)
        {
            eventHandle.WaitOne();
            if (foundList.Count() != 0)
            {
                this.BeginInvoke(new MethodInvoker(delegate
                {
                    lblLnk.Text = articleTitle;
                    lbxLinks.DataSource = foundList.pages;
                    lbxLinks.DisplayMember = "title";
                }
                    ));
            }
            else
            {
                Thread.Sleep(200);
                this.BeginInvoke(new MethodInvoker(delegate
                {
                    this.Close();
                }
                    ));
            }
        }

        

        private void lbLinks_MouseClick(object sender, MouseEventArgs e)
        {
            if (lbxLinks!=null&& lbxLinks.SelectedValue != null)
                lblResult.Text =  ((Page)lbxLinks.SelectedValue).title;
        }

        private void btnMatch_Click(object sender, EventArgs e)
        {
            Page p = new Page(UkSite, lblLnk.Text);
            lock (syncObj)
            {
                p.Load();
            }

            string linkTo = ((Page)lbxLinks.SelectedValue).title;
            if (string.IsNullOrEmpty(p.text))
            {
                CreateRedirect(p, linkTo);
            }
            else
            {
                if (
                MessageBox.Show("Such link already exists. Overrite? Contents:" + p.text, "Exists", MessageBoxButtons.YesNo)
                == DialogResult.Yes)
                {
                    CreateRedirect(p, linkTo);
                }
            }
        }

        private void CreateRedirect(Page p, string linkTo)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o) 
                {
                    p.text = string.Format("#REDIRECT [[{0}]]", linkTo);
                    lock (syncObj)
                    {
                        p.Watch();
                        p.Save("", false);
                    }
                    this.BeginInvoke(new MethodInvoker(delegate
                        {
                        lblStatus.Text = string.Format("'{0}' matched '{1}' ", lblLnk.Text, linkTo);
                        }));
                }));
            
        }

        private void MatchRedLink_Shown(object sender, EventArgs e)
        {
            MatchRedLink_Load(null, null);
        }

        private void lbxLinks_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string link = ((Page)lbxLinks.SelectedValue).title;
            OpenPage(link);
        }

        
    }
}
